# -*- coding: utf-8 -*-
from sqlalchemy import create_engine
from settings import mysql
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker


class Session(object):

    def __init__(self):
        self.mysql_url = 'mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8'.format(mysql['user'], mysql['password'],
                                                                              mysql['host'], mysql['port'],
                                                                              mysql['database'])

        self.engine = create_engine(self.mysql_url)

    def mysql_connect(self):
        # 创建会话，进行增删改查

        Session = sessionmaker(self.engine)
        session = Session()  # 实例
        return session

    def model(self):
        Base = declarative_base(self.engine)
        return Base


session = Session().mysql_connect()
Model = Session().model()
